package com.study.main.controller;

import com.study.common.entity.ResultInfo;
import com.study.common.entity.User;
import com.study.common.pagination.Pagination;
import com.study.common.utils.I18nUtils;
import com.study.common.utils.ResultUtils;
import com.study.main.entity.Information;
import com.study.main.service.MainService;
import com.study.main.vo.ConditionPagination;
import com.study.main.vo.UserInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @Create by ldr.
 * @create 2018-08-30 14:34
 * @Description：
 */
@Controller
@RequestMapping(value = "/main")
public class MainController {

    @Autowired
    @Qualifier(value = "mainServiceImpl")
    private MainService mainService;

    /**
     * 进入展示页面
     */
    @RequestMapping(value = "/show",method = RequestMethod.GET)
    public String toShow(Model m){
        //Pagination pagination = mainService.getPageByCondition(null);
        //return new ResultUtils<Pagination>().success(pagination,"");
        return "main";
    }

    /**
     * description: 条件分页查询
     * @params 查询条件
     */
    @RequestMapping(value = "/show",method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo<Pagination> doShow(@RequestBody ConditionPagination cp){
        Map<String,Object> map = new HashMap<String, Object>();
        Pagination pagination = mainService.getPageByCondition(cp);
        ResultUtils resultUtils = new ResultUtils();
        return resultUtils.success(pagination,"");
    }

    /**
     * 新增
     */
    @RequiresPermissions("all")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo<Information> addInformation(Locale locale, @RequestBody Information info){
        try {
            mainService.addInformation(info);
        } catch (Exception e) {
            return new ResultUtils().fail(I18nUtils.getrb(locale,"addFail"));
        }
        return new ResultUtils().success(info,I18nUtils.getrb(locale,"addSuccess"));
    }

    /**
     * 批量删除
     */
    @RequiresPermissions("all")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo<Information> deleteByIds(Locale locale ,@RequestBody ConditionPagination conditionPagination){
        try {
            mainService.deleteByIds(conditionPagination.getDis());
        } catch (Exception e) {
            return new ResultUtils().fail(I18nUtils.getrb(locale,"deleteFail"));
        }
        return new ResultUtils().success(null,"deleteSuccess");
    }
    /**
     * 修改权限
     */
    @RequiresPermissions("super")
    @RequestMapping(value = "/upIdentity",method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo<User> updateInfo1(Locale locale ,ConditionPagination conditionPagination){
        try {
            mainService.updateIdentity(Integer.parseInt(conditionPagination.getIdentityId()),Integer.parseInt(conditionPagination.getInfoId()));
        } catch (Exception e) {
            return new ResultUtils().fail(I18nUtils.getrb(locale,"permission")+I18nUtils.getrb(locale,"updateFail"));
        }
        return new ResultUtils().success(1,I18nUtils.getrb(locale,"permission")+I18nUtils.getrb(locale,"updateSuccess"));
    }

    /**
     * 修改用户信息
     */
    @RequiresPermissions("all")
    @RequestMapping(value = "/upInfo",method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo<Information> updateInfo(Locale locale ,@RequestBody ConditionPagination UserInfo){
        try {
            mainService.updateInfo(UserInfo);
        } catch (Exception e) {
            return new ResultUtils().fail(I18nUtils.getrb(locale,"updateFail"));
        }
        return new ResultUtils().success(1,I18nUtils.getrb(locale,"updateSuccess"));
    }
}
